package com.ptswitch.material.components;

import java.util.List;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.hxzon.tapestry5.util.RequestParameter;

import com.ptswitch.material.model.ContractItem;

public class ExamineContractItems extends AbstractField {
	@Parameter(required = true, allowNull = false)
	private List<ContractItem> value;

	@Inject
	private Request request;

	@BeginRender
	void begin(MarkupWriter writer) {
		writer.element("table", "class", "list");
		writer.element("thead");
		writer.element("tr");
		writer.element("th");
		writer.write("物料编码");
		writer.end();
		writer.element("th");
		writer.write("物料名称");
		writer.end();
		writer.element("th");
		writer.write("规格型号");
		writer.end();
		writer.element("th");
		writer.write("PCB封装");
		writer.end();
		writer.element("th");
		writer.write("数量");
		writer.end();
		writer.element("th");
		writer.write("已收货");
		writer.end();
		writer.element("th");
		writer.write("验货");
		writer.end();
		writer.end();//end tr
		writer.end();//end thead
		writer.element("tbody");
		int i = 0;
		for (ContractItem item : value) {
			writer.element("tr");
			writer.element("td");
//			writer.element("a", "class","forward","target","_blank","href",
//					request.getContextPath()+"/material/view/"+material.getId());
			writer.write(item.getCode());
//			writer.end();//end a
			writer.end();
			writer.element("td");
			writer.write(item.getName());
			writer.end();
			writer.element("td");
			writer.write(item.getSpecification());
			writer.end();
			writer.element("td");
			writer.write(item.getPcbDecal());
			writer.end();
			writer.element("td");
			writer.write(String.valueOf(item.getQuantities()));
			writer.end();
			writer.element("td");
			writer.write(String.valueOf(item.getNumReceiving()));
			writer.end();
			writer.element("td");
			writer.writeRaw("已通过"+item.getNumPass()+",未通过"+item.getNumUnPass()+"<br />");
			writer.element("input", "type", "text", "name", "item" + i + ".numPass", "value", item.getNumCurrentPass());
			writer.end();
			writer.writeRaw("通过<br />");
			writer.element("input", "type", "text", "name", "item" + i + ".numUnPass", "value", item.getNumCurrentUnPass());
			writer.end();
			writer.writeRaw("未通过<br />");
			writer.end();//end td
			writer.end();//end tr
			i++;
		}
		writer.end();//end tbody
		writer.end();//end table
	}

	@Override
	protected void processSubmission(String elementName) {
		int itemSize = value.size();
		ContractItem contractItem;
		for (int i = itemSize - 1; i >= 0; i--) {
			contractItem = value.get(i);
			int numPass = RequestParameter.getIntParameter(request, "item" + i + ".numPass");
			int numUnPass = RequestParameter.getIntParameter(request, "item" + i + ".numUnPass");
//			if (numPass > contractItem.getNumReceiving()) {
//				throw AppRuntimeException.error(contractItem.getCode() + "通过数大于已收货数");
//			}
//			if (numPass + numUnPass > contractItem.getNumReceiving()) {
//				throw AppRuntimeException.error(contractItem.getCode() + "通过数加上未通过数大于已收货数");
//			}
//			contractItem.setNumPass(contractItem.getNumPass()+numPass);
			contractItem.setNumCurrentPass(numPass);
			contractItem.setNumCurrentUnPass(numUnPass);
		}
	}
}
